﻿@using Shared.Designer.Whiteboard.Components

@{
    bool isSelectedComponent = FormDesigner.IsSelectedComponent(ComponentData);
    string selectedComponentClass = isSelectedComponent ? "component-element--selected" : "";
    string tabsContainerClass = ComponentData?.ComponentType == ComponentType.Tabs ? "container-element" : "";
}
@if (FormDesigner.IsDesigner)
{
    <div class="component-element  @selectedComponentClass @tabsContainerClass"
         @onclick="SelectComponentAsync"
         @onclick:stopPropagation="true">
        <div class="widget-wrapper">
            @if (isSelectedComponent)
            {
                <ComponentMover Component="@ComponentData"
                                ComponentsInParentRow="@ComponentRow" />
            }
            @{
                string h = "auto";
                h = ComponentData?.Height <= 0 ? "auto" : ComponentData?.Height + "px";
            }
            <div class="widget-content" style="height:@h">
                @switch (ComponentData?.ComponentType)
                {
                    case ComponentType.SingleLine:
                        <SingleLineComp Component="@ComponentData" />
                        break;

                    case ComponentType.MultiLine:
                        <MultiLineComp Component="@ComponentData" />
                        break;

                    case ComponentType.Email:
                        <EmailComp Component="@ComponentData" />
                        break;

                    case ComponentType.Link:
                        <LinkComp Component="@ComponentData" />
                        break;

                    case ComponentType.Checkbox:
                        <CheckboxComp Component="@ComponentData" />
                        break;

                    case ComponentType.Number:
                        <NumberComp Component="@ComponentData" />
                        break;

                    case ComponentType.File:
                        <FileComp Component="@ComponentData" />
                        break;

                    case ComponentType.DateTime:
                        <DateTimeComp Component="@ComponentData" />
                        break;
                    case ComponentType.Select:
                        <SelectComp Component="@ComponentData" />
                        break;
                    case ComponentType.Table:
                        <DataGridViewComp Component="@ComponentData" />
                        break;
                    case ComponentType.Tabs:
                        <TabsComp ComponentData="@ComponentData"
                                  ComponentRow="@ComponentRow"
                                  TabContainerList="@ComponentData.ChildContainers" />
                        break;

                    default:
                        throw new NotImplementedException(@$"ComponentType: '{ComponentData?.ComponentType}' is not implemented");
                }
            </div>

            @if (ComponentData?.ComponentType != ComponentType.Tabs)
            {
                //用于拖拽时候
                <DropZone Container="@Container"
                          ComponentRow="@ComponentRow"
                          ComponentIndex="@ComponentIndex"
                          ComponentData="@ComponentData" />
            }
        </div>
    </div>
}
else
{

}
@code {
    /// <summary>
    /// 整个设计界面
    /// </summary>
    [NotNull]
    [CascadingParameter(Name = "Root")]
    public FormDesigner? FormDesigner { get; set; }
    /// <summary>
    /// 父级容器
    /// </summary>
    [Parameter]
    public ContainerDto? Container { get; set; }
    /// <summary>
    /// 组件的数据
    /// </summary>
    [Parameter]
    public ComponentDto? ComponentData { get; set; }

    /// <summary>
    /// 这个组件所在的行
    /// </summary>
    [Parameter]
    public RowDto? ComponentRow { get; set; }
    /// <summary>
    /// 表示他是这一行中第几的一个组件
    /// </summary>
    [Parameter]
    public int ComponentIndex { get; set; }

  

    /// <summary>
    /// 设置本组件为FormDesigner中被选中的控件,并且刷新界面
    /// </summary>
    /// <returns></returns>
    private async Task SelectComponentAsync()
    {
        await FormDesigner.SelectComponentAsync(ComponentData);
    }
}
